extends CharacterBody2D

@export var move_speed :float = 50
@export var animator: AnimatedSprite2D
var is_game_over:bool = false

@export var bullet_sence: PackedScene

@export var FireSound : AudioStreamPlayer

@export var GameOverSound : AudioStreamPlayer

@export var RunningSound : AudioStreamPlayer

@export var RestartTimer : Timer

func _physics_process(delta):
	if not is_game_over:
		velocity = Input.get_vector("left","right","up","down") * move_speed
		if velocity == Vector2.ZERO:
			animator.play("idle")
		else:
			animator.play("run")
		move_and_slide()
		
func _process(delta: float) -> void:
	if velocity == Vector2.ZERO or is_game_over:
		RunningSound.stop()
	elif not RunningSound.playing:
		RunningSound.play()
func game_over():
	if not is_game_over:
		is_game_over = true
		animator.play("gameover")
		get_tree().current_scene.show_game_over()
		GameOverSound.play()
		RestartTimer.start()
		#await  get_tree().create_timer(3).timeout
		#get_tree().reload_current_scene()
	


func _on_fire() -> void:
	if velocity != Vector2.ZERO or is_game_over:
		return
	var bullet_node = bullet_sence.instantiate()
	bullet_node.position = position + Vector2(6,6)
	FireSound.play()
	get_tree().current_scene.add_child(bullet_node)




func _reload_sence() -> void:
	get_tree().reload_current_scene()
